package no.uio.ifi.cop.common;

import java.util.HashMap;

/**
 * ANSI colors and effects.
 *
 * Get colors and other effects on text in a
 * way that most shells understand.
 *
 * @version 1.0
 * @author bjarneh@ifi.uio.no
 */

// \033 == \u001B
public class AnsiColor{

    private static HashMap<String, String> str2color;

    static{
        init();
    }

    private static void init(){
        str2color = new HashMap<String, String>();
        str2color.put("FG_BLACK", "\u001B[30m");
        str2color.put("BLACK",  "\u001B[30m");
        str2color.put("FG_RED", "\u001B[31m");
        str2color.put("RED", "\u001B[31m");
        str2color.put("FG_GREEN", "\u001B[32m");
        str2color.put("GREEN", "\u001B[32m");
        str2color.put("FG_YELLOW", "\u001B[33m");
        str2color.put("YELLOW", "\u001B[33m");
        str2color.put("FG_BLUE", "\u001B[34m");
        str2color.put("BLUE", "\u001B[34m");
        str2color.put("FG_MAGENTA", "\u001B[35m");
        str2color.put("MAGENTA", "\u001B[35m");
        str2color.put("FG_CYAN", "\u001B[36m");
        str2color.put("CYAN", "\u001B[36m");
        str2color.put("FG_WHITE", "\u001B[37m");
        str2color.put("WHITE", "\u001B[37m");
        str2color.put("FG_DEFAULT", "\u001B[39m");
        str2color.put("BG_BLACK", "\u001B[40m");
        str2color.put("BG_RED", "\u001B[41m");
        str2color.put("BG_GREEN", "\u001B[42m");
        str2color.put("BG_YELLOW", "\u001B[43m");
        str2color.put("BG_BLUE", "\u001B[44m");
        str2color.put("BG_MAGENTA", "\u001B[45m");
        str2color.put("BG_CYAN", "\u001B[46m");
        str2color.put("BG_WHITE", "\u001B[47m");
        str2color.put("BG_DEFAULT", "\u001B[49m");
        str2color.put("BOLD", "\u001B[1m");
        str2color.put("NOBOLD",  "\u001B[22m");
        str2color.put("ITALIC", "\u001B[3m");
        str2color.put("NOITALIC", "\u001B[23m");
        str2color.put("NORMAL", "\u001B[0m");
        str2color.put("UNDERLINE", "\u001B[4m");
        str2color.put("NOUNDERLINE", "\u001B[24m");
        str2color.put("INVERSE", "\u001B[7m");
        str2color.put("NOINVERSE", "\u001B[27m");
        str2color.put("STRIKETHROUGH", "\u001B[9m");
        str2color.put("NOSTRIKETHROUGH", "\u001B[29m");
    }


    private AnsiColor(){;}

    // forground colors
    public static final String FG_BLACK        = "\u001B[30m";
    public static final String BLACK           = "\u001B[30m";
    public static final String FG_RED          = "\u001B[31m";
    public static final String RED             = "\u001B[31m";
    public static final String FG_GREEN        = "\u001B[32m";
    public static final String GREEN           = "\u001B[32m";
    public static final String FG_YELLOW       = "\u001B[33m";
    public static final String YELLOW          = "\u001B[33m";
    public static final String FG_BLUE         = "\u001B[34m";
    public static final String BLUE            = "\u001B[34m";
    public static final String FG_MAGENTA      = "\u001B[35m";
    public static final String MAGENTA         = "\u001B[35m";
    public static final String FG_CYAN         = "\u001B[36m";
    public static final String CYAN            = "\u001B[36m";
    public static final String FG_WHITE        = "\u001B[37m";
    public static final String WHITE           = "\u001B[37m";
    public static final String FG_DEFAULT      = "\u001B[39m";

    // background colors
    public static final String BG_BLACK        = "\u001B[40m";
    public static final String BG_RED          = "\u001B[41m";
    public static final String BG_GREEN        = "\u001B[42m";
    public static final String BG_YELLOW       = "\u001B[43m";
    public static final String BG_BLUE         = "\u001B[44m";
    public static final String BG_MAGENTA      = "\u001B[45m";
    public static final String BG_CYAN         = "\u001B[46m";
    public static final String BG_WHITE        = "\u001B[47m";
    public static final String BG_DEFAULT      = "\u001B[49m";



    // non-color effects
    public static final String BOLD            = "\u001B[1m";
    public static final String NOBOLD          = "\u001B[22m";
    public static final String ITALIC          = "\u001B[3m";
    public static final String NOITALIC        = "\u001B[23m";
    public static final String NORMAL          = "\u001B[0m";
    public static final String UNDERLINE       = "\u001B[4m";
    public static final String NOUNDERLINE     = "\u001B[24m";
    public static final String INVERSE         = "\u001B[7m";
    public static final String NOINVERSE       = "\u001B[27m";
    public static final String STRIKETHROUGH   = "\u001B[9m";
    public static final String NOSTRIKETHROUGH = "\u001B[29m";


    // format :  effects separated by '+' or whitespace
    // example:  FG_BLUE+STRIKETHROUGH+ITALIC+BOLD
    // example:  FG_YELLOW BOLD UNDERLINE
    public static String parse(String fmt){
        String upperCase = fmt.toUpperCase();
        String[] fmts    = upperCase.split("\\+|\\s");

        StringBuilder sb = new StringBuilder("");
        for(int i = 0; i < fmts.length; i++){
            if(str2color.containsKey( fmts[i] )){
                sb.append( str2color.get( fmts[i] ) );
            }
        }

        return sb.toString();
    }

}
